<template>
    <div>
        <Row style="margin:5px 0px 10px 5px" >
            <i-col span="2" >
                <Button  size="default"  type="primary" @click="btn_refresh">刷新</Button>
            </i-col>
            <i-col span="3" >
                <Button  size="default"  @click="btn_stop_timer" style="margin-right:10px" type="primary">停止刷新</Button> 
            </i-col>
            <i-col span="2"  >
                <Button  size="default"  @click="btn_get_problem_pods" style="margin-right:10px" type="primary">故障Pod</Button>
            </i-col>
		</Row>
        <i-table size="default"   border stripe  :columns="format" :data="show_list" :height="sharedState.tableHeight" >
            <template slot-scope="{ row, index }" slot="action">
                <Dropdown>
                    <a href="javascript:void(0)">
                        <Icon type="ios-create-outline" size="30" />
                    </a>
                    <DropdownMenu slot="list" placement="top-end">
                        <DropdownItem><Button  size="default"    @click="btn_get_log(index)" long>日志</Button></DropdownItem>
                        <DropdownItem><Button  size="default"    @click="del_namespace_pod(index)" long>删除</Button></DropdownItem>
                    </DropdownMenu>
                </Dropdown>
            </template>
        </i-table >
        <div style="text-align: center;margin-top: 10px;">
            <Page ref="page" :total="total" :page-size="pageSize"  @on-change="changePage" @on-page-size-change="changePageSize" show-total show-sizer/>
        </div>
    </div>
</template>
<script>

import {k8s_namespace_post} from '@/api/http'
import { delete_pod,get_namespaced_pod_list,get_problem_pod_list} from  '@/api'
import { 
    delete_namespace_resource,
    // show_simple_page 
}from '@/common/util.js'
import store from '@/store'

export default {
    data() {
        return {
            sharedState: store.state,
            format: [
                {
                    title: '名字',key: 'name',align:'center',width:200,fixed:'left',
                    render: (h,params) => {
                        return h('div',[
                            h('a',{
                                style: {
                                    color: 'blue'
                                },
                                on: {
                                    click: ()=>{
                                        this.handleLink(params);
                                    }
                                }
                            },params.row.name)
                        ])
                    }
                },
                {
                    title: '命名空间',key: 'namespace',align: 'center',width: 150,
                },

                {
                    title: '状态',key: 'status',align: 'center',width: 100,
                    render: (h,params) => {
                        let status = params.row.status
                        let color = ''
                        switch(status){
                            case 'Running':
                                color = '#228B22'
                                break
                            case 'Pending':
                                color = '#ff9900'
                                break
                            case 'Succeeded':
                                color = '#2b85e4'
                                break
                            case 'Failed':
                                color = '#ed4014'
                                break
                            case 'Error':
                                color = '#ed4014'
                                break
                            case 'Unknown':
                                color = '#2db7f5'
                                break
                            default:
                                color = '#2db7f5'
                        }
                        return h('span',{style: {color: color,}},status)
                    }
                },
                {
                    'title':'重启',key:'restart_count',align: 'center',width: 100,
                    render:(h,params) =>{
                        let value = params.row.restart_count+'次'
                        return h('p',value)
                    }
                },
                {
                    title: 'CPU',key:'pod_usage',sortable: true,align: 'center',width: 100,
                    sortMethod:function(a,b,type) {
                        if (type == 'asc') {
                            return  a['cpu']< b['cpu'] ? -1 : 1

                        } else {
                            return  a['cpu']> b['cpu'] ? -1 : 1
                        }
                    },
                    render: (h,params) =>{
                        let pod_usage = params.row.pod_usage
                        if (pod_usage == 'null' || pod_usage==null) {
                            return h('p','无')
                        }else{
                            let cpu_usage = pod_usage.cpu
                            let value=''
                            if(cpu_usage < 1000){
                                value = cpu_usage + "m"
                            }else{
                                value = (cpu_usage/1000).toFixed(1)+"c"
                            }
                            return h('p',value)
                        }
                    }
                },
                {
                    title: '内存',key:'pod_usage',sortable: true,align: 'center',width: 100,
                    sortMethod:function(a,b,type) {
                        if (type == 'asc') {
                            return  a['memory']< b['memory'] ? -1 : 1

                        } else {
                            return  a['memory']> b['memory'] ? -1 : 1
                        }
                    },
                    render: (h,params) =>{
                        let pod_usage = params.row.pod_usage
                        if (pod_usage == 'null' || pod_usage==null) {
                            return h('p','无')
                        }else{
                            let memory_usage = pod_usage.memory
                            let value=''
                            if(memory_usage < 1024){
                                value = memory_usage + "M"
                            }else{
                                value = (memory_usage/1024).toFixed(1)+"G"
                            }
                            return h('p',value)
                        }
                    }   
                },
                {
                    title: 'Pod请求',key: 'requests',align: 'left',width: 180,
                    render: (h, params) => {
                        let resources = params.row.resources
                        let requests = resources.requests
                        let req_cpu = requests['cpu']
                        let req_memory = requests['memory']
                        let cpu=''
                        if(req_cpu==0){
                            cpu = '无'
                        }else if(req_cpu < 1000){
                            cpu = req_cpu + "m"
                        }else{
                            cpu = (req_cpu/1000).toFixed(1)+"c"
                        }
                        let memory=''
                        if(req_memory==0){
                            memory = '无'
                        }else if(req_memory < 1024){
                            memory = req_memory + "M"
                        }else{
                            memory = (req_memory/1024).toFixed(1)+"G"
                        }
                        let value = "cpu:"+cpu+"\n"+"memory:"+memory
                        return h('pre',value)

                    }

                },
                {
                    title: 'Pod限制',key: 'limits',align: 'left',width: 180,
                    render: (h, params) => {
                        let resources = params.row.resources
                        let limits = resources.limits
                        let limit_cpu = limits['cpu']
                        let limit_memory = limits['memory']
                        let cpu=''
                        if(limit_cpu==0){
                            cpu = '无'
                        }else if(limit_cpu < 1000){
                            cpu = limit_cpu + "m"
                        }else{
                            cpu = (limit_cpu/1000).toFixed(1)+"c"
                        }
                        let memory=''
                        if(limit_memory == 0){
                            memory = '无'
                        }else if(limit_memory < 1024){
                            memory = limit_memory + "M"
                        }else{
                            memory = (limit_memory/1024).toFixed(1)+"G"
                        }
                        let value = "cpu:"+cpu+"\n"+"memory:"+memory
                        return h('pre',value)

                    }
                },
                // {
                //     title: '容器使用',key: 'container_usage',width:300,
                //     render: (h, params) => {
                //         return h('div', [
                //             h('pre', JSON.stringify(params.row.container_usage,undefined,4))
                //         ]);
                //     }

                // },

                {
                    title: '节点',key: 'node',align: 'center',width: 180,
                },
                {
                    title: 'pod_ip',key: 'pod_ip',align: 'center',width: 150,
                },
                {
                    title: '镜像',key: 'image',width:200,
                },
                {
                    title: '创建时间',key: 'create_time',align: 'center',width: 150,
                },
                {
                    title: '操作',slot: 'action',align: 'center',width: 150,fixed:'right'
                }
            ],
            // total_list: [],
            show_list: [],
            total: 0,
            pageSize: 10,
            page: 1,
            is_problem: 0
        }
    },
    methods: {
        btn_refresh(){
            this.refresh()
        },
        btn_get_problem_pods(){
            this.is_problem = 1
            this.refresh()
        },
        // 链接点击事件，进入详情页
        handleLink(params){
            let namespace = params.row.namespace
            // this.$router.push({name: 'podDetail',query: {name: params.row.name,namespace: namespace}})
            const { href } = this.$router.resolve({
                name: 'podDetail'
            });
            window.open(href+"?name="+params.row.name+"&namespace="+namespace, '_blank');
            // 停掉定时

        },
        btn_get_log(index){
            let name = this.show_list[index].name
            let namespace =  this.show_list[index].namespace
            // const { href } = this.$router.resolve({
            //     name: 'podLog'
            // });
            // window.open(href+"?name="+name+"&namespace="+namespace);
            this.$router.push({name: 'podLog',query: {name: name,namespace: namespace}})
            console.log("stop timer")
            clearInterval(this.timer);
        },
        changePage(page) {
            this.page=page
            this.refresh()
        },
        // 每页条数变更
        changePageSize(pageSize){
            console.log(pageSize)
            this.pageSize = pageSize
            this.refresh()
        },
        del_pod(namespace,pod_name){
            let name = pod_name
            let result = confirm("确定要删除"+name+"吗?")
            if(result == false) return 
            delete_namespace_resource(namespace,name,delete_pod)
            this.refresh()
        },
        del_namespace_pod(index){
            let pod_name = this.show_list[index].name
            let namespace = this.show_list[index].namespace
            if(pod_name && namespace){
                this.del_pod(namespace,pod_name)
            }
        },

        // 刷新按命名空间显示的pod列表
        refresh(){
            let data = {
                "pageSize":this.pageSize,
                "page":this.page,
            }
            let url = null
            if(this.is_problem){
                url = get_problem_pod_list
            }else{
                url = get_namespaced_pod_list
            }
            k8s_namespace_post(url,data,{},30000).then( (response) => {
                let data = response.data
                this.total  = data.total
                this.show_list = data.pod_list
            })
        },
        btn_stop_timer(){
            clearInterval(this.timer);
        },

    },
    mounted() {
        this.refresh()
        this.timer = setInterval(this.refresh, 10000);
    },
    beforeDestroy() {
        clearInterval(this.timer);
    }
}
</script>
<style>
    pre{
        white-space: pre-wrap!important;
        word-wrap: break-word!important;
        *white-space:normal!important;
    }

</style>